import { core, getImage, RenderableBehaviour } from "../gameengine";
import { getPlatform } from "../platform";

export class AnimationRenderer extends RenderableBehaviour {

    private image:HTMLImageElement;
    private config:any;
    private duringTime:number = 0;
    private currentFrame:number = 0;
    filepath: string;

    protected onStart() {
        this.displayObject.renderNode = this;

         getPlatform().loadText('assets/output.json',(text)=>{
            this.config = JSON.parse(text);
            core.loadImage('assets/output.png')
            this.image =  getImage('assets/output.png')
        })

       

    }    
    protected onUpdate(advancedTime: number) {
      
        if (!this.config){
            return;
        }
        this.duringTime += advancedTime;
        this.currentFrame = (Math.floor(this.duringTime / 16 /4)) % 8;
    }

    onRender(context:CanvasRenderingContext2D){
        if (this.config){
            const url = this.currentFrame + 1 + ".png"
            const rect = this.config[url];
            console.log(url)
            context.drawImage(this.image,rect.x,rect.y,rect.width,rect.height,rect.offsetX,rect.offsetY,rect.width,rect.height);
        }
       

    }


}